﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Devfw.OpenAuth;
using System.Xml;

namespace Devfw.OpenAuth.Platform
{
    public class Douban : ProviderBase
    {
        private string _contentType = "application/atom+xml";
        private Encoding _contentEncoding = Encoding.UTF8;

        public Douban( string oauth_consumer_key, string oauth_consumer_secret )
            : base( oauth_consumer_key, oauth_consumer_secret,  "1.0", SignatureType.HMACSHA1, AuthorizationType.UrlQuery )
        {
        }

        public Douban( string oauth_consumer_key, string oauth_consumer_secret, string oauth_token, string oauth_token_secret )
            : base( oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret )
        {
        }


        #region OAuth
        public RequestTokenResponse request_token()
        {
            _core.AuthorizationType = AuthorizationType.HttpAuthorizationHeader;
            return _core.request_token("http://www.douban.com/service/auth/request_token", null, new ParameterCollection()
                {
                    new Parameter("realm","http://sp.example.com/", ParameterType.OAuth,false),
                });
        }

        public void authorize( string oauth_token, string oauth_callback )
        {
            string url = _core.generate_authorize_url( "http://www.douban.com/service/auth/authorize", oauth_token, oauth_callback );
            HttpContext.Current.Response.Redirect( url );
        }

        public string GetAuthorizeUrl(string oauth_token, string oauth_callback)
        {
            if (!string.IsNullOrEmpty(oauth_token))
            {
                return _core.generate_authorize_url("http://www.douban.com/service/auth/authorize", oauth_token, oauth_callback);
            }

            return string.Empty;
        }

        public AccessTokenResponse access_token( string oauth_token, string oauth_token_screct, string oauth_verfier )
        {
            return _core.access_token( "http://www.douban.com/service/auth/access_token", oauth_token, oauth_token_screct, oauth_verfier );
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="alt">atom/json</param>
        /// <param name="content"></param>
        /// <returns></returns>
        public string saying( string alt, string content )
        {
            string format =  string.Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?><entry xmlns:ns0=\"http://www.w3.org/2005/Atom\" xmlns:db=\"http://www.douban.com/xmlns/\"><content>{0}</content></entry> ",content );

            string response = _core.OpenAuthWebRequest( HttpWebMethod.POST, "http://api.douban.com/miniblog/saying", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new Parameter("realm","http://sp.example.com/", ParameterType.OAuth,false),
                    new Parameter( "", string.Format( format, content ), ParameterType.Post ,false, false)
                }, AuthorizationType.HttpAuthorizationHeader, _contentType , _contentEncoding );

            return response;
        }

        public string GetUserInfo(string userID)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, string.Format("http://api.douban.com/people/{0}",userID), OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                } , AuthorizationType.UrlQuery);
            return response;
        }
    }
}
